import request from '@/utils/request';

export interface ChildCategoryItem {
  categoryId: string;
  categoryName: string;
}

export interface CategoryItem {
  categoryId: string;
  categoryName: string;
  serNum: number;
  childCategoryList?: ChildCategoryItem[];
}

export interface ListResponse {
  list: CategoryItem[];
  total: number;
}

export async function qryMainCategoryListPage(params: {
  pageIndex?: number;
  pageSize?: number;
  param?: {
    categoryName?: string;
    [key: string]: any;
  };
}): Promise<ListResponse> {
  const response = await request('/product/qryMainCategoryListPage', {
    method: 'POST',
    data: {
      pageIndex: params.pageIndex || 1,
      pageSize: params.pageSize || 10,
      param: params.param || {},
    },
  });
  return {
    list: response.data || [],
    total: response.total || 0,
  };
}

export async function addMainCategory(params: {
  categoryName: string;
  serNum: number;
}) {
  return request('/product/addMainCategory', {
    method: 'POST',
    data: params,
  });
}

export async function updateMainCategory(params: {
  categoryId: string;
  categoryName: string;
  serNum: number;
}) {
  return request('/product/updateMainCategory', {
    method: 'POST',
    data: params,
  });
}

export async function deleteMainCategory(params: { categoryId: string }) {
  return request('/product/deleteMainCategory', {
    method: 'POST',
    data: params,
  });
}

export async function qryMainCategoryDetail(params: {
  categoryId: string;
}): Promise<CategoryItem> {
  const response = await request('/product/qryMainCategoryDetail', {
    method: 'POST',
    data: params,
  });
  return response.data;
}
